package com.ymatou.iapi.csim.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.csim.parameter.OpenSessionBean;
import com.ymatou.iapi.csim.parameter.RevolveSessionBean;
import com.ymatou.iapi.csim.service.OpenSessionCall;
import com.ymatou.iapi.csim.service.RevolveSessionCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CsimCallService;
import com.ymttest.database.model.ymt_cssession;
import com.ymttest.database.model.ymt_onlinecs;
import com.ymttest.database.sqlwapper.CSIM.ymt_cssessionWapper;
import com.ymttest.database.sqlwapper.CSIM.ymt_onlinecsWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
/**
*A客服主动转接给B客服
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_RevolveSession{
	private static RevolveSessionBean bean;
	private static RevolveSessionCall call;
	
	private CsimCallService service=new CsimCallService();
	private Integer csIdA=Integer.parseInt(TestDataManager.getRandomNum(5));
	private Integer csIdB=Integer.parseInt(TestDataManager.getRandomNum(5));
	private Integer userId=Integer.parseInt(EnvSetup.getData("userId_IM").toString());
	private static ymt_cssessionWapper csSessionWapper=new ymt_cssessionWapper();
	private static ymt_onlinecsWapper onlinecsWapper=new ymt_onlinecsWapper();
	private ymt_onlinecs onlinecsA_old=new ymt_onlinecs();
	private ymt_onlinecs onlinecsB_old=new ymt_onlinecs();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("A客服主动转接给B客服");
	}

	@Before
	public void caseUp() throws InterruptedException {
		bean=new RevolveSessionBean();
		call=new RevolveSessionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RevolveSession_001() {
		Logger.start(true,"A客服主动转接给B客服,验证B在线且允许转接");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csIdA);
			//csIdB上线
			ymt_onlinecs onlinecs2=service.getOnlineCS(csIdB);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csIdA,userId);
			csIdA=csSession.getCSId();//这里给csidA赋值 目的为了，当前的用户已经在会话中，直接把当前的csId赋值给csIdA
			onlinecsA_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdA,"Status",0)).get(0);
			onlinecsB_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdB,"Status",0)).get(0);
			
			bean.setFromCSId(csIdA);
			bean.setToCSId(csIdB);
			bean.setSessionId(csSession.getId());
			call.setData(bean);
			call.callService();		
			verifyReturn();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RevolveSession_002() {
		Logger.start(true,"A客服主动转接给B客服，验证B不在线");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csIdA);
			//csIdB上线
			//ymt_onlinecs onlinecs2=service.getOnlineCS(csIdB);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csIdA,userId);
			csIdA=csSession.getCSId();//这里给csidA赋值 目的为了，当前的用户已经在会话中，直接把当前的csId赋值给csIdA
			onlinecsA_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdA,"Status",0)).get(0);
			//onlinecsB_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdB,"Status",1)).get(0);
			
			bean.setFromCSId(csIdA);
			bean.setToCSId(csIdB);
			bean.setSessionId(csSession.getId());
			call.setData(bean);
			call.callService();	
			
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("客会主动转接会话--对方客服不在线"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getResult().get("Message").toString().contains("客会主动转接会话--对方客服不在线"), "验证返回的Message");
			Logger.verifyEquals(true,call.getMsg().toString().contains("客会主动转接会话--对方客服不在线"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RevolveSession_003() {
		Logger.start(true,"A客服主动转接给B客服，验证B不允许转接(B是买手客服，A是买家客服)");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csIdA);
			//csIdB上线
			ymt_onlinecs onlinecs2=service.getOnlineCS_Seller(csIdB);
//			onlinecs2.setCSType(1);
//			onlinecsWapper.updateByPrimaryKey(onlinecs2);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csIdA,userId);
			//ymt_cssession csSession2=service.getOpenSession(csIdB,userId);
			csIdA=csSession.getCSId();//这里给csidA赋值 目的为了，当前的用户已经在会话中，直接把当前的csId赋值给csIdA
			onlinecsA_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdA,"Status",0)).get(0);
			onlinecsB_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdB,"Status",0)).get(0);
			
			bean.setFromCSId(csIdA);
			bean.setToCSId(csIdB);
			bean.setSessionId(csSession.getId());
			call.setData(bean);
			call.callService();		
			
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("客会主动转接会话--不允许的客服类型，没有办法发起转接"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getResult().get("Message").toString().contains("客会主动转接会话--不允许的客服类型，没有办法发起转接"), "验证返回的Message");
			Logger.verifyEquals(true,call.getMsg().toString().contains("客会主动转接会话--不允许的客服类型，没有办法发起转接"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RevolveSession_004() {
		Logger.start(true,"A客服主动转接给B客服,验证会话不存在");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csIdA);
			//csIdB上线
			ymt_onlinecs onlinecs2=service.getOnlineCS(csIdB);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csIdA,userId);
			csIdA=csSession.getCSId();//这里给csidA赋值 目的为了，当前的用户已经在会话中，直接把当前的csId赋值给csIdA
			onlinecsA_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdA,"Status",0)).get(0);
			onlinecsB_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdB,"Status",0)).get(0);
			
			bean.setFromCSId(csIdA);
			bean.setToCSId(csIdB);
			bean.setSessionId(csSession.getId()+1);
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("客会主动转接会话--会话不存在，或者会话已经关闭"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getResult().get("Message").toString().contains("客会主动转接会话--会话不存在，或者会话已经关闭"), "验证返回的Message");
			Logger.verifyEquals(true,call.getMsg().toString().contains("客会主动转接会话--会话不存在，或者会话已经关闭"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_RevolveSession_005() {
		Logger.start(true,"A客服主动转接给B客服,验证会话已经关闭");
		try {
			//csIdA上线
			ymt_onlinecs onlinecs=service.getOnlineCS(csIdA);
			//csIdB上线
			ymt_onlinecs onlinecs2=service.getOnlineCS(csIdB);
			//csIdA 主动会话
			ymt_cssession csSession=service.getOpenSession(csIdA,userId);
			csIdA=csSession.getCSId();//这里给csidA赋值 目的为了，当前的用户已经在会话中，直接把当前的csId赋值给csIdA
			onlinecsA_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdA,"Status",0)).get(0);
			onlinecsB_old=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdB,"Status",0)).get(0);
			//关闭会话
			csSession.setStatus(1);
			csSessionWapper.updateByPrimaryKey(csSession);
			
			bean.setFromCSId(csIdA);
			bean.setToCSId(csIdB);
			bean.setSessionId(csSession.getId());
			call.setData(bean);
			call.callService();		
			Logger.verifyEquals("2",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
			Logger.verifyEquals(true,call.getResult().get("ErrorMessage").toString().contains("客会主动转接会话--会话不存在，或者会话已经关闭"), "验证返回的errorMessage");
			Logger.verifyEquals(false,call.getResult().get("Success"), "验证返回的Success");
			Logger.verifyEquals(true,call.getResult().get("Message").toString().contains("客会主动转接会话--会话不存在，或者会话已经关闭"), "验证返回的Message");
			Logger.verifyEquals(true,call.getMsg().toString().contains("客会主动转接会话--会话不存在，或者会话已经关闭"), "验证返回的Msg");
			Logger.verifyEquals("500",call.getStatus(), "验证返回的status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	private void verifyReturn() throws InterruptedException
	{
		Logger.verifyEquals("0",call.getResult().get("ErrorCode").toString(), "验证返回的errorCode");
		Logger.verifyEquals("",call.getResult().get("ErrorMessage"), "验证返回的errorMessage");
		Logger.verifyEquals(true,call.getResult().get("Success"), "验证返回的Success");
		Logger.verifyEquals(true,call.getResult().get("Message").toString().contains("会话转接成功"), "验证返回的Message");
		Logger.verifyEquals("",call.getMsg().toString(), "验证返回的Msg");
		Logger.verifyEquals("200",call.getStatus(), "验证返回的status");
		
		//数据库验证
		//创建新会话 B
		ymt_cssession csSessionB=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csIdB,"UserId",userId,"Status",0));
		Logger.verifyEquals(csIdA, csSessionB.getFromCSId(), "验证数据库FromCSId会话来源是csIdA");
		Logger.verifyEquals(0, csSessionB.getStatus(), "验证数据库会话打开，Status=0");
		//新会话 ymt_onlinecs currentSession+1
		ymt_onlinecs onlinecsB=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdB,"Status",0)).get(0);
		Logger.verifyEquals(1, onlinecsB.getCurrentSessions()-onlinecsB_old.getCurrentSessions(), "验证数据库新会话 B加1");
		//老会话关闭 A
		ymt_cssession csSessionA=csSessionWapper.selectBy(MapUtil.hashMap("CSId",csIdA,"UserId",userId));
		Logger.verifyEquals(1, csSessionA.getStatus(), "验证数据库会话关闭，Status=1");
		//老会话 ymt_onlinecs currentSession-1
		ymt_onlinecs onlinecsA=onlinecsWapper.selectBy(MapUtil.hashMap("CSId",csIdA,"Status",0)).get(0);
		Logger.verifyEquals(1, onlinecsA_old.getCurrentSessions()-onlinecsA.getCurrentSessions(), "验证数据库新会话 A减1");
	}
}